package cn.edu.tsinghua.cic.ccss.web;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.poi.ss.usermodel.Workbook;
import org.forten.core.ro.DtoResultWithPageInfo;
import org.forten.dataio.excel.ExcelUtil;
import org.forten.dataio.excel.exporter.Excel2003Exporter;
import org.forten.dataio.excel.exporter.Excel2007Exporter;
import org.forten.dataio.excel.exporter.ExcelExporter;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.SessionAttributes;

import cn.edu.tsinghua.cic.ccss.bo.CourseSelectBo;
import cn.edu.tsinghua.cic.ccss.dto.CourseSelectionDto;
import cn.edu.tsinghua.cic.ccss.dto.SelectedCourseDto4Export;
import cn.edu.tsinghua.cic.ccss.qo.CourseSelectionQo;

/**管理端快捷功能界面
 * @author cj
 *
 */
@Controller
@Scope("prototype")
@RequestMapping("selectcourse")
@SessionAttributes("courseSelectionQo")
public class CourseAction4SelectionQuickAdmin {

	@Resource(name = "courseSelectionQo")
	private CourseSelectionQo courseSelectionQo;
	@Resource(name = "courseSelectBo")
	private CourseSelectBo courseSelectBo;
	private String message = null;

	@RequestMapping("gotoCourseSelectByAdminList")
	public String gotoCourseSelectByAdmin(Model model) {
		model.addAttribute("courseSelectionQo", courseSelectionQo);
		return "forward:showCourseSelectByAdminList.jspx";
	}

	@RequestMapping("showCourseSelectByAdminList")
	public String showCourseSelectByAdminList(
			Model model,
			@RequestParam(defaultValue = "1") int pageNo,
			@ModelAttribute("courseSelectionQo") CourseSelectionQo courseSelectionQo) {
		DtoResultWithPageInfo<CourseSelectionDto> dtoList = this.courseSelectBo
				.queryCourseSelection(courseSelectionQo, pageNo);
		model.addAttribute("dto", dtoList.getResults());
		model.addAttribute("page", dtoList.getPageInfo());
		model.addAttribute("courseSelectionQo", courseSelectionQo);
		return "selectcourse/gldxk_gbtk";
	}

	@RequestMapping("cancelCourse")
	public void cancelCourse(HttpServletResponse response,
			@RequestParam Long selectedCourseId,
			@RequestParam String cancelReason) throws IOException {
		this.courseSelectBo.cancelCourse(selectedCourseId, cancelReason);
		message = "操作成功！";
		courseSelectionQo.clean();
		response.setHeader("Cache-Control", "no-cache");
		response.setContentType("text/html;charset=utf-8");
		response.getWriter().write(message);
		response.getWriter().flush();
//		return "forward:gotoCourseSelectByAdminList.jspx";
	}
	
	@RequestMapping("leaveQuick")
	public void leaveQuick(HttpServletResponse response,
			@RequestParam Long selectedCourseId,
			@RequestParam String leaveReason,
			@RequestParam String state) throws IOException{
		this.courseSelectBo.leave4Admin(selectedCourseId, state, leaveReason);
		response.setHeader("Cache-Control", "no-cache");
		response.setContentType("text/html;charset=utf-8");
		response.getWriter().write(state);
		response.getWriter().flush();
//		return "forward:gotoCourseSelectByAdminList.jspx";
	}
	
	@RequestMapping("export2007")
	public void export2007(HttpServletResponse response,
			HttpServletRequest request,
			@ModelAttribute("courseSelectionQo") CourseSelectionQo courseSelectionQo){
		List<SelectedCourseDto4Export> result = courseSelectBo.export(courseSelectionQo);
		ExcelExporter export = new Excel2007Exporter();
		Workbook wb = export.export(result);
		if (wb == null) {
			RequestDispatcher view = request
					.getRequestDispatcher("../WEB-INF/pages/error-msg.jsp");
			try {
				List<String> errMsg = new ArrayList<String>();
				errMsg.add("导出数据结果集为空");
				request.setAttribute("errMsg", errMsg);
				request.setAttribute("url", "gotoCourseSelectByAdminList.jspx");
				view.forward(request, response);
			} catch (ServletException e) {
				e.printStackTrace();
			} catch (IOException e) {
				e.printStackTrace();
			}
		} else {
			ExcelUtil.downloadFile(response, wb,
					"SelectedCourseInfo" + export.getPostfix());
		}
	}
	
	@RequestMapping("export2003")
	public void export2003(HttpServletResponse response,
			HttpServletRequest request,
			@ModelAttribute("courseSelectionQo") CourseSelectionQo courseSelectionQo){
		List<SelectedCourseDto4Export> result = courseSelectBo.export(courseSelectionQo);
		ExcelExporter export = new Excel2003Exporter();
		Workbook wb = export.export(result);
		if (wb == null) {
			RequestDispatcher view = request
					.getRequestDispatcher("../WEB-INF/pages/error-msg.jsp");
			try {
				List<String> errMsg = new ArrayList<String>();
				errMsg.add("导出数据结果集为空");
				request.setAttribute("errMsg", errMsg);
				request.setAttribute("url", "gotoCourseSelectByAdminList.jspx");
				view.forward(request, response);
			} catch (ServletException e) {
				e.printStackTrace();
			} catch (IOException e) {
				e.printStackTrace();
			}
		} else {
			ExcelUtil.downloadFile(response, wb,
					"SelectedCourseInfo" + export.getPostfix());
		}
	}
}
